<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>员工管理</title>
<#include "../common/link.ftl"/>
    <script>
        // 移动所有
        function moveAll(srcSelect, distSelect) {
            $("." + srcSelect + " option").appendTo($("." + distSelect));
        }
        // 移动选中
        function moveSelected(srcSelect, distSelect) {
            $("." + srcSelect + " option:selected").appendTo($("." + distSelect));
        }

        //在用户提交表单的时候,我们将右边框中的所有选项选中
        $(function () {

            //在表单提交的时候,会先来执行这里的函数
            $("#editForm").submit(function () {
                $(".selfRoles option").prop("selected", true);
            })
        })
        //页面加载完之后,将右边框中分配给当前用户的角色从左边删除
        $(function () {
            //获取右边框中option的value,存到数组中
            var ids = [];
            $.each($(".selfRoles option"), function (index, option) {
                ids.push(option.value);
            })
            //遍历左边框中的所有option,判断其value值是否在上面的数组中
            //在:删除
            $.each($(".allRoles option"), function (index, option) {
                if ($.inArray(option.value, ids) != -1) {
                    $(option).remove();
                }
            })
            var role = "";
            //超级管理员
            $("#admin").change(function () {
                if (this.checked) {
                    role = $("#role").detach();
                } else {
                    $(this).closest(".form-group").after(role);
                }
            })
            //编辑时候， 根据是否是超级管理员决定是否显示下拉框
        <#if e?? && e.admin>
            role = $("#role").detach();
        </#if>
            // 数据校验
            $("#editForm").validate({
                rules: {
                    name: {
                        required: true,
                        minlength: 2
                    },
                    password: {
                        required: true,
                        minlength: 3
                    },
                    repassword:{
                        equalTo:"#password"
                    },
                    email:{
                        required: true,
                        email:true
                    },
                    age:{
                        required: true,
                        range:[18,80]
                    }
                },
                messages: {
                    name:{
                        required: "账号不能为空",
                        minlength: "最少2位"
                    },
                    password: {
                        required: "密码不能为空",
                        minlength: "最少3位"
                    },
                    repassword: {
                        equalTo: "两次密码不一致"
                    },
                    email: {
                        required: "邮箱不能为空",
                        email: "邮箱格式不正确"
                    },
                    age: {
                        required: "年龄不能为空",
                        range: "年龄范围在18-80岁之间"
                    }
                }
            })
        })
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<#include "../common/navbar.ftl" />
    <!--菜单回显-->
    <#assign currentMenu="employee"/>
<#include "../common/menu.ftl" />
    <div class="content-wrapper">
        <section class="content-header">
            <h1>员工编辑</h1>
        </section>
        <section class="content">
            <div class="box">
                <form class="form-horizontal" action="/employee/saveOrUpdate" method="post" id="editForm">
                    <input type="hidden" value="${(e.id)!}" name="id">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-2 control-label">用户名：</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="name" name="name" value="${(e.name)!}"
                                   placeholder="请输入员工名">
                        </div>
                    </div>
                <#if !e??>
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="password" class="col-sm-2 control-label">密码：</label>
                        <div class="col-sm-4">
                            <input type="password" class="form-control" id="password" name="password"
                                   value="${(e.password)!}"
                                   placeholder="请输入员工密码">
                        </div>
                    </div>

                    <div class="form-group" style="margin-top: 10px;">
                        <label for="repassword" class="col-sm-2 control-label">验证密码：</label>
                        <div class="col-sm-4">
                            <input type="password" class="form-control" id="repassword" name="repassword"
                                   placeholder="请输入验证密码">
                        </div>
                    </div>
                </#if>
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="email" class="col-sm-2 control-label">电子邮箱：</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="email" name="email" value="${(e.email)!}"
                                   placeholder="请输入电子邮箱">
                        </div>
                    </div>

                    <div class="form-group" style="margin-top: 10px;">
                        <label for="age" class="col-sm-2 control-label">年龄：</label>
                        <div class="col-sm-4">
                            <input type="text" class="form-control" id="age" name="age" value="${(e.age)!}"
                                   placeholder="请输入年龄">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="dept" class="col-sm-2 control-label">部门：</label>
                        <div class="col-sm-4">
                            <select class="form-control" id="dept" name="dept.id">
                            <#list depts as d>
                                <option value="${d.id}">${d.name}</option>
                            </#list>
                            </select>
                        </div>
                    </div>
                    <script>
                        /*为下拉框回显数据*/
                        $("select[name='dept.id']").val(${(e.dept.id)!});
                    </script>
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="admin" class="col-sm-2 control-label">超级管理员：</label>
                        <div class="col-sm-4">
                            <input type="checkbox" id="admin" name="admin">
                        </div>
                    </div>
                    <script>
                        $("input[name=admin]").prop("checked",${(e?? && e.admin)?string("true","false")});
                    </script>
                    <div class="form-group " id="role">
                        <label for="role" class="col-sm-2 control-label">分配角色：</label><br/>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-2 col-sm-offset-2">
                                <select multiple class="form-control allRoles" size="15">
                                <#list roles as r>
                                    <option value="${r.id}">${r.name}</option>
                                </#list>
                                </select>
                            </div>
                            <div class="col-sm-1" style="margin-top: 60px;" align="center">
                                <div>

                                    <a type="button" class="btn btn-primary  " style="margin-top: 10px" title="右移动"
                                       onclick="moveSelected('allRoles', 'selfRoles')">
                                        <span class="glyphicon glyphicon-menu-right"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="左移动"
                                       onclick="moveSelected('selfRoles', 'allRoles')">
                                        <span class="glyphicon glyphicon-menu-left"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全右移动"
                                       onclick="moveAll('allRoles', 'selfRoles')">
                                        <span class="glyphicon glyphicon-forward"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全左移动"
                                       onclick="moveAll('selfRoles', 'allRoles')">
                                        <span class="glyphicon glyphicon-backward"></span>
                                    </a>
                                </div>
                            </div>

                            <div class="col-sm-2">
                                <select multiple class="form-control selfRoles" size="15" name="roleIds">
                                <#if e?? && e.roles??>
                                    <#list e.roles as r>
                                        <option value="${r.id}">${r.name}</option>
                                    </#list>
                                </#if>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <button id="submitBtn" type="submit" class="btn btn-primary">保存</button>
                            <button type="reset" class="btn btn-danger">重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </section>
    </div>
<#include "../common/footer.ftl" />
</div>
</body>
</html>
